﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.其他窗体
{
    public partial class 图片显示 : Form
    {
         public string DanjuBianhao { get; set; }
        public Image Image { get; set; }
        public List<MadanPicture > Madans { get; set; }
        private int Index = 0;
        public 图片显示()
        {
            InitializeComponent();
        }

        private void 图片显示_Load(object sender, EventArgs e)
        {
            label2.Text = DanjuBianhao;
            Madans = MadanPictureService.GetMadanPicturelst(x => x.ckdh == DanjuBianhao);
            if (Madans.Count > 0)
            {
                
                pictureBox1.Image = Tools.ImgHelp.BytesToImage(Madans[0].picture);
            }
            else
            { pictureBox1.Image = null; }
        }

        private void 适应大小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void 全部显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void 图片居中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "图片|*.jpg;*.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Image fromImage = Image.FromFile(openFileDialog1.FileName);
                //fromImage = fromImage.AdjImageToFitSize(pictureBox1.Width, pictureBox1.Height); //350
                PictureBoxEx.loadImg(pictureBox1, openFileDialog1.FileName);
                Madans.Add(new MadanPicture() { ckdh = DanjuBianhao, picture = Tools.ImgHelp.ImageToBytes(pictureBox1.Image), rq =DateTime.Now ,khbh=string.Empty  });
            }
            Index = Madans.Count - 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (Madans.Count > 0)
                { 
                    if (Index == Madans.Count - 1)
                    {
                        MessageBox.Show("已经是最后一张图片了");
                    }
                    else
                    {
                        Index++;
                        pictureBox1.Image = Tools.ImgHelp.BytesToImage(Madans[Index].picture);
                    }
            }
                else
                {
                    MessageBox.Show("没有任何图片");
                }
            }
            catch (Exception ex)
            {
                Sunny.UI.UIMessageBox.ShowError(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (Madans.Count > 0)
                {
                    if (Index == 0)
                    {
                        MessageBox.Show("已经是第一张图片了");
                    }
                    else
                    {
                        Index--;
                        pictureBox1.Image = Tools.ImgHelp.BytesToImage(Madans[Index].picture);
                    }
                }
                else
                {
                    MessageBox.Show("没有任何图片");
                }
            }
            catch(Exception ex)
            {
                Sunny.UI.UIMessageBox.ShowError(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MadanPictureService.DeleteMadanPicture(x => x.ckdh == DanjuBianhao);
            MadanPictureService.InsertMadanPicturelst(Madans);
            Sunny.UI.UIMessageBox.ShowSuccess("保存成功");
            this.Close();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Clipboard.SetImage(pictureBox1.Image);
            }
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //IDataObject iData = Clipboard.GetDataObject();
            //if (iData.GetDataPresent(DataFormats.Bitmap))
            //{
            //    pictureBox1.Image = (Bitmap)iData.GetData(DataFormats.Bitmap);
            //}
            //else
            //{
            //    try
            //    {
            //        pictureBox1.Image = (Bitmap)iData.GetData(DataFormats.Bitmap);
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //        return;
            //    }
            //}
            try
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.MetafilePict))
                {
                    var img = Clipboard.GetImage();
                    pictureBox1.Tag = Guid.NewGuid();
                    pictureBox1.Image = img;
                }
                else if (iData.GetDataPresent(DataFormats.FileDrop))
                {
                    var files = Clipboard.GetFileDropList();
                    if (files.Count == 0) { return; }
                    pictureBox1.Tag = Guid.NewGuid();
                    pictureBox1.Image = Image.FromFile(files[0]);
                }

                else if (iData.GetDataPresent(DataFormats.Text))
                {
                    var path = (String)iData.GetData(DataFormats.Text);
                    var chars = Path.GetInvalidPathChars();
                    if (path.IndexOfAny(chars) >= 0)
                    {
                        MessageBox.Show("路径中包含非法字符！");
                        return;
                    }
                    if (System.IO.File.Exists(path))
                    {
                        var name = Path.GetFileNameWithoutExtension(path);
                        var extension = path.Substring(path.LastIndexOf("."));
                        string imgType = ".png|.jpg|.jpeg";
                        if (imgType.Contains(extension.ToLower()))
                        {
                            pictureBox1.Image = Image.FromFile(path);
                            pictureBox1.Tag = Guid.NewGuid();

                        }
                        else
                        {
                            MessageBox.Show("格式错误！");
                        }

                    }
                    else
                    {
                        MessageBox.Show("文件不存在！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("粘贴图片失败"+ex.Message);
                return;
            }
            Madans.Add(new MadanPicture() { ckdh = DanjuBianhao, picture = Tools.ImgHelp.ImageToBytes(pictureBox1.Image), rq = DateTime.Now, khbh = string.Empty });
        }

        private void 删除图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Index>=0)
            {
                pictureBox1.Image = Tools.ImgHelp.BytesToImage(Madans[Index].picture);
                MadanPictureService.DeleteMadanPicture(x=>x.ID == Madans[Index].ID);
                Madans.RemoveAt(Index);
            }
            else
            {
                pictureBox1.Image = null;
            }
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "图片保存";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp";
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();
                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;
                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            default:
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                        }
                        try
                        {
                            Bitmap bit = new Bitmap(pictureBox1.BackgroundImage);
                            MessageBox.Show(fileName);
                            pictureBox1.BackgroundImage.Save(fileName, imgformat);
                        }
                        catch(Exception ex)
                        {
                            Sunny.UI.UIMessageBox.ShowError(ex.Message);
                        }
                    }
                }
            }
        }
    }
}
